home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / graphic / jpegsrc4.zip / EXAMPLE.C < prev    next >
C/C++ Source or Header  |  1992-11-14  |  28KB  |  632 lines

  1. /*
  2.  * example.c
  3.  *
  4.  * This file is not actually part of the JPEG software.  Rather, it provides
  5.  * a skeleton that may be useful for constructing applications that use the
  6.  * JPEG software as subroutines.  This code will NOT do anything useful as is.
  7.  *
  8.  * This file illustrates how to use the JPEG code as a subroutine library
  9.  * to read or write JPEG image files.  We assume here that you are not
  10.  * merely interested in converting the image to yet another image file format
  11.  * (if you are, you should be adding another I/O module to cjpeg/djpeg, not
  12.  * constructing a new application).  Instead, we show how to pass the
  13.  * decompressed image data into or out of routines that you provide.  For
  14.  * example, a viewer program might use the JPEG decompressor together with
  15.  * routines that write the decompressed image directly to a display.
  16.  *
  17.  * We present these routines in the same coding style used in the JPEG code
  18.  * (ANSI function definitions, etc); but you are of course free to code your
  19.  * routines in a different style if you prefer.
  20.  */
  21.  
  22. /*
  23.  * Include file for declaring JPEG data structures.
  24.  * This file also includes some system headers like <stdio.h>;
  25.  * if you prefer, you can include "jconfig.h" and "jpegdata.h" instead.
  26.  */
  27.  
  28. #include "jinclude.h"
  29.  
  30. /*
  31.  * <setjmp.h> is used for the optional error recovery mechanism shown in
  32.  * the second part of the example.
  33.  */
  34.  
  35. #include <setjmp.h>
  36.  
  37.  
  38.  
  39. /******************** JPEG COMPRESSION SAMPLE INTERFACE *******************/
  40.  
  41. /* This half of the example shows how to feed data into the JPEG compressor.
  42.  * We present a minimal version that does not worry about refinements such
  43.  * as error recovery (the JPEG code will just exit() if it gets an error).
  44.  */
  45.  
  46.  
  47. /*
  48.  * To supply the image data for compression, you must define three routines
  49.  * input_init, get_input_row, and input_term.  These routines will be called
  50.  * from the JPEG compressor via function pointer values that you store in the
  51.  * cinfo data structure; hence they need not be globally visible and the exact
  52.  * names don't matter.  (In fact, the "METHODDEF" macro expands to "static" if
  53.  * you use the unmodified JPEG include files.)
  54.  *
  55.  * The input file reading modules (jrdppm.c, jrdgif.c, jrdtarga.c, etc) may be
  56.  * useful examples of what these routines should actually do, although each of
  57.  * them is encrusted with a lot of specialized code for its own file format.
  58.  */
  59.  
  60.  
  61. METHODDEF void
  62. input_init (compress_info_ptr cinfo)
  63. /* Initialize for input; return image size and component data. */
  64. {
  65.   /* This routine must return five pieces of information about the incoming
  66.    * image, and must do any setup needed for the get_input_row routine.
  67.    * The image information is returned in fields of the cinfo struct.
  68.    * (If you don't care about modularity, you could initialize these fields
  69.    * in the main JPEG calling routine, and make this routine be a no-op.)
  70.    * We show some example values here.
  71.    */
  72.   cinfo->image_width = 640;        /* width in pixels */
  73.   cinfo->image_height = 480;        /* height in pixels */
  74.   /* JPEG views an image as being a rectangular array of pixels, with each
  75.    * pixel having the same number of "component" values (color channels).
  76.    * You must specify how many components there are and the colorspace
  77.    * interpretation of the components.  Most applications will use RGB data or
  78.    * grayscale data.  If you want to use something else, you'll need to study
  79.    * and perhaps modify jcdeflts.c, jccolor.c, and jdcolor.c.
  80.    */
  81.   cinfo->input_components = 3;        /* or 1 for grayscale */
  82.   cinfo->in_color_space = CS_RGB;    /* or CS_GRAYSCALE for grayscale */
  83.   cinfo->data_precision = 8;        /* bits per pixel component value */
  84.   /* In the current JPEG software, data_precision must be set equal to
  85.    * BITS_IN_JSAMPLE, which is 8 unless you twiddle jconfig.h.  Future
  86.    * versions might allow you to say either 8 or 12 if compiled with
  87.    * 12-bit JSAMPLEs, or up to 16 in lossless mode.  In any case,
  88.    * it is up to you to scale incoming pixel values to the range
  89.    *   0 .. (1<<data_precision)-1.
  90.    * If your image data format is fixed at a byte per component,
  91.    * then saying "8" is probably the best long-term solution.
  92.    */
  93. }
  94.  
  95.  
  96. /*
  97.  * This function is called repeatedly and must supply the next row of pixels
  98.  * on each call.  The rows MUST be returned in top-to-bottom order if you want
  99.  * your JPEG files to be compatible with everyone else's.  (If you cannot
  100.  * readily read your data in that order, you'll need an intermediate array to
  101.  * hold the image.  See jrdtarga.c or jrdrle.c for examples of handling
  102.  * bottom-to-top source data using the JPEG code's portable mechanisms.)
  103.  * The data is to be returned into a 2-D array of JSAMPLEs, indexed as
  104.  *        JSAMPLE pixel_row[component][column]
  105.  * where component runs from 0 to cinfo->input_components-1, and column runs
  106.  * from 0 to cinfo->image_width-1 (column 0 is left edge of image).  Note that
  107.  * this is actually an array of pointers to arrays rather than a true 2D array,
  108.  * since C does not support variable-size multidimensional arrays.
  109.  * JSAMPLE is typically typedef'd as "unsigned char".
  110.  */
  111.  
  112.  
  113. METHODDEF void
  114. get_input_row (compress_info_ptr cinfo, JSAMPARRAY pixel_row)
  115. /* Read next row of pixels into pixel_row[][] */
  116. {
  117.   /* This example shows how you might read RGB data (3 components)
  118.    * from an input file in which the data is stored 3 bytes per pixel
  119.    * in left-to-right, top-to-bottom order.
  120.    */
  121.   register FILE * infile = cinfo->input_file;
  122.   register JSAMPROW ptr0, ptr1, ptr2;
  123.   register long col;
  124.   
  125.   ptr0 = pixel_row[0];
  126.   ptr1 = pixel_row[1];
  127.   ptr2 = pixel_row[2];
  128.   for (col = 0; col < cinfo->image_width; col++) {
  129.     *ptr0++ = (JSAMPLE) getc(infile); /* red */
  130.     *ptr1++ = (JSAMPLE) getc(infile); /* green */
  131.     *ptr2++ = (JSAMPLE) getc(infile); /* blue */
  132.   }
  133. }
  134.  
  135.  
  136. METHODDEF void
  137. input_term (compress_info_ptr cinfo)
  138. /* Finish up at the end of the input */
  139. {
  140.   /* This termination routine will very often have no work to do, */
  141.   /* but you must provide it anyway. */
  142.   /* Note that the JPEG code will only call it during successful exit; */
  143.   /* if you want it called during error exit, you gotta do that yourself. */
  144. }
  145.  
  146.  
  147. /*
  148.  * That's it for the routines that deal with reading the input image data.
  149.  * Now we have overall control and parameter selection routines.
  150.  */
  151.  
  152.  
  153. /*
  154.  * This routine must determine what output JPEG file format is to be written,
  155.  * and make any other compression parameter changes that are desirable.
  156.  * This routine gets control after the input file header has been read
  157.  * (i.e., right after input_init has been called).  You could combine its
  158.  * functions into input_init, or even into the main control routine, but
  159.  * if you have several different input_init routines, it's a definite win
  160.  * to keep this separate.  You MUST supply this routine even if it's a no-op.
  161.  */
  162.  
  163. METHODDEF void
  164. c_ui_method_selection (compress_info_ptr cinfo)
  165. {
  166.   /* If the input is gray scale, generate a monochrome JPEG file. */
  167.   if (cinfo->in_color_space == CS_GRAYSCALE)
  168.     j_monochrome_default(cinfo);
  169.   /* For now, always select JFIF output format. */
  170.   jselwjfif(cinfo);
  171. }
  172.  
  173.  
  174. /*
  175.  * OK, here is the main function that actually causes everything to happen.
  176.  * We assume here that the target filename is supplied by the caller of this
  177.  * routine, and that all JPEG compression parameters can be default values.
  178.  */
  179.  
  180. GLOBAL void
  181. write_JPEG_file (char * filename)
  182. {
  183.   /* These three structs contain JPEG parameters and working data.
  184.    * They must survive for the duration of parameter setup and one
  185.    * call to jpeg_compress; typically, making them local data in the
  186.    * calling routine is the best strategy.
  187.    */
  188.   struct Compress_info_struct cinfo;
  189.   struct Compress_methods_struct c_methods;
  190.   struct External_methods_struct e_methods;
  191.  
  192.   /* Initialize the system-dependent method pointers. */
  193.   cinfo.methods = &c_methods;    /* links to method structs */
  194.   cinfo.emethods = &e_methods;
  195.   /* Here we use the default JPEG error handl